package gui;

import gui.AbstractViewPane.ViewMode;
import gui.nodes.AbstractNode;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import com.toedter.calendar.JCalendar;

import persistence.db.DataSet;
import business.SemestreBL;

public class SemestreUIConsulter extends AbstractViewPane {
	
	private int id = -1;
	private int idAnneeEtude;
	private String nom;
	private String debut;
	private String fin;
	private DataSet groupeSet = null;
	private DataSet ueSet = null;
	
	private JTextField fieldNom;
	private JCalendar fieldDebut;
	private JCalendar fieldFin;
			
	private int dayDeb;
	private int monthDeb;
	private int yearDeb;
	private int dayFin;
	private int monthFin;
	private int yearFin;

	private ViewMode currentViewMode;
	private ComponentList componentList = new ComponentList();
	private AbstractNode creator;
	
    public void initGUI (int idSemestre) {

        //si l'id du noeud envoyé est -1, on est en mode ajout, les champs seront donc vides
        if(currentViewMode != ViewMode.AJOUT){
        	SemestreBL semestreBL = new SemestreBL(idSemestre);
			nom = semestreBL.getNomSemestre();
			debut = semestreBL.getDebutSemestre();
			fin = semestreBL.getFinSemestre();
        }

        
        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());

        JLabel l = new JLabel("Nom du semestre :", JLabel.TRAILING);
        p.add(l);
        fieldNom = new JTextField(nom);
        l.setLabelFor(fieldNom);
        p.add(fieldNom);
        componentList.add(fieldNom);
        
        l = new JLabel("Debut du semestre: ", JLabel.TRAILING);
        p.add(l);
        if (currentViewMode != ViewMode.AJOUT) {
	    	String tmp = debut.replace('/',' ');
	    	StringTokenizer tokenizer = new StringTokenizer(tmp);
	   	    dayDeb = Integer.parseInt(tokenizer.nextToken());
	   	    monthDeb = Integer.parseInt(tokenizer.nextToken());
	   	    yearDeb = Integer.parseInt(tokenizer.nextToken());
	        Date date = new Date(yearDeb-1900, monthDeb-1, dayDeb);
	        fieldDebut = new JCalendar(date);
        }
        else
        	fieldDebut = new JCalendar();
        l.setLabelFor(fieldDebut);
        p.add(fieldDebut);
        componentList.add(fieldDebut);

        l = new JLabel("Fin du semestre: ", JLabel.TRAILING);
        p.add(l);
        if (currentViewMode != ViewMode.AJOUT) {
	        String tmp = fin.replace('/',' ');
	        StringTokenizer tokenizer = new StringTokenizer(tmp);
	   	    dayFin = Integer.parseInt(tokenizer.nextToken());
	   	    monthFin = Integer.parseInt(tokenizer.nextToken());
	   	    yearFin = Integer.parseInt(tokenizer.nextToken());
	        Date date = new Date(yearFin-1900, monthFin-1, dayFin);
	        fieldFin = new JCalendar(date);
        }
        else 
        	fieldFin = new JCalendar();
        l.setLabelFor(fieldFin);
        p.add(fieldFin);
        componentList.add(fieldFin);

        //Lay out the panel.
        SpringUtilities.makeCompactGrid(p,
                                        3, 2, //rows, cols
                                        6, 6,        //initX, initY
                                        6, 6);       //xPad, yPad
        
        add(p, BorderLayout.NORTH);
	    
        if (currentViewMode != ViewMode.CONSULTATION) {
        	// Mode modification ou ajout - afficher les boutons
			showCommandBox();
        }
        else {
        	componentList.disableAll();
        }
    	
    }
    private void showCommandBox() {
    	JPanel commandBox = new JPanel();
		JButton save = new JButton("Enregistrer");
		save.setActionCommand("Enregistrer");
		save.addActionListener(new SemestreListener());
		commandBox.add(save);
		drawCommandBox(commandBox);
    }
	
	public SemestreUIConsulter(AbstractNode creator, ViewMode v, int idSemestre, int idAE) {
		this.creator = creator;
		currentViewMode = v;
		id = idSemestre;
		this.idAnneeEtude = idAE;
		initGUI (idSemestre);
	}
	
	@Override
	public String getTitle() {
		// TODO Auto-generated method stub
		if (currentViewMode == ViewMode.CONSULTATION)
			return "Consultation d'un semestre";
		else if (currentViewMode == ViewMode.AJOUT)
			return "Ajout d'un semestre";
		else
			return "Modification d'un semestre";
	}

	@Override
	public boolean hasBeenModified() {
		// TODO Auto-generated method stub
		return (currentViewMode != ViewMode.CONSULTATION);
	}

	//inner class chargée de gérer l'écoute du bouton d'enregistrement du formulaire
	private class SemestreListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			if(arg0.getActionCommand().equals("Enregistrer")){
				System.out.println("Demande d'enregistrement");
				SemestreBL semestreSave = new SemestreBL();
				//si -1, c'est un ajout d'enseignant, sinon modification de l'enseignant de l'id concerné
				if(currentViewMode == ViewMode.AJOUT){
						semestreSave.ajouterSemestre(getID(), getNom(), getDebut(), getFin(), 0, 1, getIdAnneeEtude(), getUESet(), getGroupeSet());
						componentList.disableAll();
				} else {
						semestreSave.modifierSemestre(getID(), getNom(), getDebut(), getFin(), 0, 1, getIdAnneeEtude(), getUESet(), getGroupeSet());
						componentList.disableAll();
				}
				creator.refresh();
			}	
		}
	}
	
	//accesseurs
	public int getID() {
		return id;
	}
	
	public int getIdAnneeEtude () {
		return idAnneeEtude;
	}
	
	public String getNom() {
		return fieldNom.getText();
	}
	
	public String getDebut() {
		return fieldDebut.getDayChooser().getDay()+"/"+fieldDebut.getMonthChooser().getMonth()+"/"+fieldDebut.getYearChooser().getYear();
	}

	public String getFin() {
		return fieldFin.getDayChooser().getDay()+"/"+fieldFin.getMonthChooser().getMonth()+"/"+fieldFin.getYearChooser().getYear();
	}
	
	public DataSet getGroupeSet(){
		return groupeSet;
	}
	
	public DataSet getUESet(){
		return ueSet;
	}
	

}
